package edu.whut.mall.admin.service.impl;

import edu.whut.mall.admin.dto.PageDto;
import edu.whut.mall.admin.dto.ProductDto;
import edu.whut.mall.admin.service.IFileManageAdminService;
import edu.whut.mall.admin.service.IProductService;
import edu.whut.mall.common.enums.FileType;
import edu.whut.mall.common.enums.ProductFileType;
import edu.whut.mall.common.mybatis.ParamsMap;
import edu.whut.mall.common.vo.Page;
import edu.whut.mall.pojo.mapper.FileMapper;
import edu.whut.mall.pojo.mapper.ProductFileMapper;
import edu.whut.mall.pojo.mapper.ProductMapper;
import edu.whut.mall.common.vo.ProductTypeVo;
import edu.whut.mall.common.vo.ProductVo;
import edu.whut.mall.pojo.model.File;
import edu.whut.mall.pojo.model.Product;
import edu.whut.mall.pojo.model.ProductFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-07 16:15
 */
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private IFileManageAdminService fileService;

    @Resource
    private ProductFileMapper productFileMapper;


    @Override
    public ProductTypeVo getProductType(int id) {
        log.info("类型" + productMapper.getProductType(id));
        return productMapper.getProductType(id);
    }

    @Override
    public ProductVo getProduct(int id) {
        return productMapper.getProduct(id);
    }

    @Override
    public Page<ProductVo> getAllProductList(PageDto pageDto, Integer merchantId) {
        ParamsMap map = ParamsMap.getPageInstance(pageDto.getCurrent(), pageDto.getSize(), pageDto.getOrderBy(), pageDto.getAsc());
        map.put("search", pageDto.getSearch());
        map.put("merchantId", merchantId);
        //开始查询记录和数量
        List<ProductVo> list = productMapper.getAllProductList(map);
        Integer count = productMapper.findCountProductPage(map);
        return new Page<>(list, count, pageDto.getCurrent(), pageDto.getSize());
    }

    @Override
    public Integer createProduct(ProductDto productDto) {
        Product product = productDto;
        product.setIsDeleted(false);
        product.setCreateTime(new Date());
        productMapper.insert(product);

        //产品基本介绍图片
        List<File> file = new ArrayList<>(), fileThumb = new ArrayList<>();
        if (productDto.getFileList() != null){
            log.info("fileListsize:"+productDto.getFileList().length);
            for (int i=0;i<productDto.getFileList().length;i++){
                file.add(fileService.addOneFile(productDto.getFileList()[i]));
                fileThumb.add(fileService.addOneFile(productDto.getFileThumbList()[i]));
            }
        }

        if (file.size() == fileThumb.size()) {
            for (int i = 0; i < file.size(); i++) {
                ProductFile productFile = new ProductFile(file.get(i).getId(), fileThumb.get(i).getId(), product.getId(),
                        ProductFileType.产品基本介绍图片);
                productFileMapper.insert(productFile);
            }
        }
        //产品详情图片
        List<File> infofileList = new ArrayList<>(), infofileThumbList = new ArrayList<>();
        if (productDto.getInfofileList() !=null){
            log.info("infofileListsize:"+productDto.getInfofileList().length);
            for (int i=0;i<productDto.getInfofileList().length;i++){
                infofileList.add(fileService.addOneFile(productDto.getInfofileList()[i]));
                infofileThumbList.add(fileService.addOneFile(productDto.getInfofileThumbList()[i]));
            }
        }

        if (infofileList.size() == infofileThumbList.size()) {
            for (int i = 0; i < infofileList.size(); i++) {
                ProductFile productFile = new ProductFile(infofileList.get(i).getId(), infofileThumbList.get(i).getId(), product.getId(),
                        ProductFileType.产品详情图片);
                productFileMapper.insert(productFile);
            }
        }
        //产品列表展示图片
        List<File> listfileList = new ArrayList<>(), listfileThumbList = new ArrayList<>();
        if (productDto.getListfileList() != null) {
            log.info("listfileListsize:"+productDto.getListfileList().length);
            for (int i = 0; i < productDto.getListfileList().length; i++) {
                listfileList.add(fileService.addOneFile(productDto.getListfileList()[i]));
                listfileThumbList.add(fileService.addOneFile(productDto.getListfileThumbList()[i]));
            }

        }

        if (listfileList.size() == listfileThumbList.size()) {
            for (int i = 0; i < listfileList.size(); i++) {
                ProductFile productFile = new ProductFile(listfileList.get(i).getId(), listfileThumbList.get(i).getId(), product.getId(),
                        ProductFileType.产品列表展示图片);
                productFileMapper.insert(productFile);
            }
        }

        return null;
    }
}
